<?php

    #-------------------------------------------------------------#
	# This file is part of the Tekuna MVC Microframework for PHP. #
	# (c) 2009 Niels Jäckel <niels.jaeckel@tekuna.org>            #
	# Please visit http://tekuna.org/ for more information.       #
	#                                                             #
	# This software is licensed under the MIT License. Please     #
	# view the LICENSE file for the full text of this license.    #
    #-------------------------------------------------------------#


	Tekuna :: load('org.tekuna.core.application.ApplicationContextException');
	Tekuna :: load('org.tekuna.core.request.Request');
	Tekuna :: load('org.tekuna.core.response.Response');
	Tekuna :: load('org.tekuna.core.request.ClientInformation');
	Tekuna :: load('org.tekuna.core.request.URLInformation');
	Tekuna :: load('org.tekuna.core.config.ApplicationConfiguration');


	/**
	 * The TekunaApplicationContext holds references to all objects, that
	 * influence the current request. These are:
	 *
	 *   - Request
	 *   - Response
	 *   - ClientInformation
	 *   - URLInformation
	 *   - ApplicationConfiguration
	 *   - the current language
	 *   - an array of freely-settable context values
	 */
	class TekunaApplicationContext {

		protected
			$objRequest = NULL,
			$objResponse = NULL,
			$objClientInformation = NULL,
			$objURLInformation = NULL,
			$objApplicationConfiguration = NULL,
			$sLanguage = '',
			$arrContextValues = array();


		/**
		 * Set the request object.
		 *
		 * @param Request $objRequest
		 */

		public function setRequest(Request $objRequest) {

			$this -> objRequest = $objRequest;
		}


		/**
		 * @return Request returns the request object.
		 */

		public function getRequest() {

			return $this -> objRequest;
		}


		/**
		 * Set the response object.
		 *
		 * @param Response $objResponse
		 */

		public function setResponse(Response $objResponse) {

			$this -> objResponse = $objResponse;
		}


		/**
		 * @return Response returns the response object.
		 */

		public function getResponse() {

			return $this -> objResponse;
		}


		/**
		 * Set the client information object.
		 *
		 * @param ClientInformation $objClientInformation
		 */

		public function setClientInformation(ClientInformation $objClientInformation) {

			$this -> objClientInformation = $objClientInformation;
		}


		/**
		 * @return ClientInformation returns the client information object.
		 */

		public function getClientInformation() {

			return $this -> objClientInformation;
		}


		/**
		 * Set the url information object.
		 *
		 * @param URLInformation $objURLInformation
		 */

		public function setURLInformation(URLInformation $objURLInformation) {

			$this -> objURLInformation = $objURLInformation;
		}


		/**
		 * @return URLInformation returns the url information object.
		 */

		public function getURLInformation() {

			return $this -> objURLInformation;
		}


		/**
		 * Set the application configuration object.
		 *
		 * @param ApplicationConfiguration $objApplicationConfiguration
		 */

		public function setApplicationConfiguration(ApplicationConfiguration $objApplicationConfiguration) {

			$this -> objApplicationConfiguration = $objApplicationConfiguration;
		}


		/**
		 * @return ApplicationConfiguration returns the application configuration object.
		 */

		public function getApplicationConfiguration() {

			return $this -> objApplicationConfiguration;
		}


		/**
		 * Set the application language. If the language is not supported
		 * by the application configuration, a ApplicationContextException is thrown.
		 *
		 * @param string $sLanguage
		 */

		public function setApplicationLanguage($sLanguage) {

			// get supported languages
			$objConfig = $this -> getApplicationConfiguration();

			// load languages if configuration available
			if (is_object($objConfig)) {

				// get languages
				$arrLanguages = $objConfig -> getLanguages();

				// check if the language is supported
				if (! in_array($sLanguage, $arrLanguages)) {

					throw new ApplicationContextException("The language '$sLanguage' is not supported by the application.");
				}
			}

			$this -> sLanguage = $sLanguage;
		}


		/**
		 * @return string returns the application language.
		 */

		public function getApplicationLanguage() {

			return $this -> sLanguage;
		}


		/**
		 * Set a context value.
		 *
		 * @param string $sKey the key for identification
		 * @param string $mValue the actual context value (any type)
		 */

		public function setContextValue($sKey, $mValue) {

			$this -> arrContextValues[$sKey] = $mValue;
		}


		/**
		 * Returns the value of the specified context value. If
		 * no context value exists for the given key, a
		 * ApplicationContextException is thrown.
		 *
		 * @param string $sKey the key of the context value
		 * @return mixed the actual context value
		 */

		public function getContextValue($sKey) {

			if ($this -> contextValueExists($sKey)) {

				return $this -> arrContextValues[$sKey];
			}
			else {

				throw new ApplicationContextException("The context value '$sKey' does not exist.");
			}
		}


		/**
		 * @return array returns all registered context values
		 */

		public function getAllContextValues() {

			return $this -> arrContextValues;
		}


		/**
		 * Checks, if a specified context value exists.
		 *
		 * @param string $sKey the key of the context value
		 * @return boolean returns TRUE, if the context value is set
		 */

		public function contextValueExists($sKey) {

			return isset($this -> arrContextValues[$sKey]);
		}


		/**
		 * Returns an associative array of values, that will be available in
		 * the application layout template. This array consists of all context
		 * values and the application language as 'applicationLanguage' key.
		 *
		 * @return array associative array of values
		 */

		public function getLayoutValues() {

			$arrValues = $this -> getAllContextValues();
			$arrValues['applicationLanguage'] = $this -> getApplicationLanguage();

			return $arrValues;
		}
	}


